<?php
	/**
 	 * System permissions columns class
 	 */
	class SystemPermissionsColumnsController extends Controller
	{
		public $page_title = LANG_SYSTEM_PERMISSIONS_COLUMNS;
		public $layout = 'main';
		public $module = 'administration';
		public $helpers = array ('html', 'htmlx', 'toolbars', 'grids');
		public $name = 'system_permissions_columns';
		public $use_logging = false;

		/**
		 * Edits system permissions columns
		 *
		 * @param bigint $role_id role id
		 * @param bigint $system_table_id system table id
		 */	
		function Edit($role_id, $system_table_id = null)
		{
			$this->data['role_id'] = $role_id;
			$this->data['system_table_id'] = $system_table_id;
			$role_object = $this->GetModelObject('roles');
			$this->data['role'] = $role_object->Get($role_id);
			$this->data['permissions'] = $this->controller_model->ListItems(array("role_id" => $role_id, "system_table_id" => $system_table_id));

			if (!empty($this->request->post))
			{
				$this->savePermissions($this->request->post,$role_id);
				$this->redirect('system_access/edit/'.$role_id);
			}
			else
			{

			}	
		}
				
		/**
		 * Saves system permissions columns
		 *
		 * @param array $data data array
		 * @param bigint $role_id role_id role id
		 */	
		function SavePermissions($data,$role_id)
		{
			$edit_list = '0';
			$view_list = '0';
			foreach ($data['permission'] as $key => $value)
			{
				if ($value['edit'] == 1) $edit_list = $edit_list.', '.$key;
				if ($value['view'] == 1) $view_list = $view_list.', '.$key;
			}
			$this->controller_model->Update(array('system_table_id' => $data['system_table_id'], 'role_id' => $role_id, 'edit_list' => $edit_list, 'view_list' => $view_list), 'sp_system_permissions_columns_update_list');
		}
		
		/**
		 * View system permissions columns
		 *
		 * @param bigint $role_id role id
		 * @param bigint $system_table_id system table id
		 */	
		function View($role_id, $system_table_id)
		{
			$this->data['role_id'] = $role_id;
			$this->data['system_table_id'] = $system_table_id;
			$role_object = $this->GetModelObject('roles');
			$this->data['role'] = $role_object->Get($role_id);
			$this->data['permissions'] = $this->controller_model->ListItems(array("role_id" => $role_id, "system_table_id" => $system_table_id));
		}
	}
?>